package com.seari.tunnel.controller;


import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.DisabledAccountException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.web.filter.authc.FormAuthenticationFilter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.jframe.util.ExceptionsUtils;
import com.seari.tunnel.entity.ResultMessage;
import com.seari.tunnel.service.IUserService;


@Controller
@RequestMapping("/login")
public class LoginController {

	private static final Logger LOG = LoggerFactory.getLogger(LoginController.class); 
	
	private final String LOGIN_PAGE = "/login";
	
	private final String TIMEOUT_PAGE = "/timeout";
	
	@Autowired
	IUserService userService;
	
	@RequestMapping(method = RequestMethod.GET)
	public String login() {
		
		return LOGIN_PAGE;
	}
	
	@RequestMapping(value="/logout",method = RequestMethod.GET)
	public String logout() {
		Subject subject = SecurityUtils.getSubject();
		if (subject.isAuthenticated()) {
			subject.logout(); // session 会销毁，在SessionListener监听session销毁，清理权限缓存
		}
		return LOGIN_PAGE;
	}
	
	
	@RequestMapping(params="ajax=true")
	@ResponseBody
	public Object jsonTimeOut()  {
		
		return  new ResultMessage("登录超时,请重新登录！",false);
	}
	
	
	
	/**
	 * easyui登录超时
	 * @return
	 */
	@RequestMapping(method = {RequestMethod.GET}, params="ajax=true",headers="Accept=text/html")
	public ModelAndView htmlTimeout()  {
		 
		return new ModelAndView(TIMEOUT_PAGE);
	}
	
	
	@RequestMapping(method = RequestMethod.POST)
	public ModelAndView fail(@RequestParam(FormAuthenticationFilter.DEFAULT_USERNAME_PARAM) String username,
			Map<String, Object> map, HttpServletRequest request) {
		
		String message = parseException(request);
		return new ModelAndView(LOGIN_PAGE)
			.addObject("username",username)
			.addObject("message",message);
	}
	private String parseException(HttpServletRequest request) {
		String errorString = (String)request.getAttribute(FormAuthenticationFilter.DEFAULT_ERROR_KEY_ATTRIBUTE_NAME);
		Class<?> error = null;
		try {
			if (errorString != null) {
				error = Class.forName(errorString);
			}
		} catch (ClassNotFoundException e) {
			LOG.error(ExceptionsUtils.getStackTraceAsString(e));
		} 
		
		String msg = "其他错误！";
		if (error != null) {
			if (error.equals(UnknownAccountException.class))
				msg = "未知帐号错误！";
			else if (error.equals(IncorrectCredentialsException.class))
				msg = "密码错误！";
			else if (error.equals(AuthenticationException.class))
				msg = "认证失败！";
			else if (error.equals(DisabledAccountException.class))
				msg = "账号被冻结！";
		}
		return "登录失败，" + msg;
	}
}
